package tpenchere.controllers;

import java.util.Date;

import javax.swing.JOptionPane;

import org.jdesktop.application.Action;

import tpenchere.model.Enchere;
import tpenchere.model.Produit;
import tpenchere.service.EnchereServiceOperations;
import tpenchere.ui.AbstractView;
import tpenchere.ui.contents.HomeWindow;
import tpenchere.ui.model.ProductTableModel;
import tpenchere.client.impl.*;

public class HomeController extends DefaultController {
	private final HomeWindow view;
	
	private class HomeProductListener extends PushListener {
		public HomeProductListener(String channel) {
			super(channel);
		}

		@Override
		public void update(PushEvent event) {
			System.out.println("Updating product list");
			HomeController.this.loadProductList();
		}
	}
	
	public HomeController(HomeWindow pView) {
		view = pView;
		EnchereServiceOperations serviceImpl = provider.getService();
		
		// Subscribe to product list channel
		serviceImpl.subscribe(provider.getClient(), "productList");
		
		// Add listener for push
		provider.getClientImpl().addPushListener(new HomeProductListener("productList"));
	}
	
	@Action(name = "view.home.toolbar.new")
	public void newProduct() {
		this.switchToView(AbstractView.ViewType.BID);
	}
	
	@Action(name = "view.home.toolbar.participate")
	public void participate() {
		Produit product;
		int row = view.getProductList().getSelectedRow();
		if(row == -1) {
			view.showError("Select a product in the list.");
			return;
		}
		Integer productId = (Integer) view.getProductsModel().getValueAt(row, 0);
		String input = JOptionPane.showInputDialog("Enter bid for product "+productId+":");
		EnchereServiceOperations serviceImpl = provider.getService();
		Enchere enchere = new Enchere();
		enchere.idProduit = productId;
		enchere.pseudo = this.session.getLoggedUser().pseudo;
		try {
			enchere.prix = Float.parseFloat(input);			
		} catch (Exception e) {
			view.showError("Input a valid numeric value.");
			return;
		}
		
		try {
			serviceImpl.saveEnchere(enchere);
		} catch (IllegalArgumentException iae) {
			view.showError("You have input a price that is below the current bid !");
		} catch(Exception e) {
			view.showError("An error has occured");
			return;
		}
		
	}

	@Action(name = "view.home.toolbar.details")
	public void details () {
		int row = view.getProductList().getSelectedRow();
		if(row == -1) {
			view.showError("Select a product in the list.");
			return;
		}
		Integer productId = (Integer) view.getProductsModel().getValueAt(row, 0);
		this.switchToView("BidHistoryWindow", Integer.valueOf(1));
	}
	
	public void loadProductList() {
		EnchereServiceOperations serviceImpl = provider.getService();
		Produit[] products = serviceImpl.getAllProduct();
		HomeWindow v = (HomeWindow)view;
		ProductTableModel newModel = new ProductTableModel();
		
		ProductTableModel mod = v.getProductsModel();
		mod.clearData();
		
		for(int i = 0; i < products.length; i++) {
			mod.insertRowProduct(products[i]);
		}
		System.out.println("Revalidating table");
		view.getProductList().revalidate();
	}
}
